<template>
  <view class="content-center">
<!--    <van-loading size="36" vertical color="#0081ff">加载中...</van-loading>-->
    <view class="margin-bottom-xl">
      <view class="text-blue margin-bottom-lg" v-if="this.util.isNotEmpty(lxUserInfo)">当前登录蓝信用户信息:{{lxUserInfo.loginName}}</view>
      <view class="text-blue margin-bottom-lg">测试环境用户切换界面,正式上线后会移除</view>

    </view>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
                v-model="systemKey"
                name="用户名"
                label="用户名"
                placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
                v-model="systemFlag"
                type="password"
                name="密码"
                label="密码"
                placeholder="密码"
                readonly
                :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary"  native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </view>
</template>

<script>
export default {
  name: "index",
  components: {},
  props: {},

  onLoad: async function (option) {
    await this.$onLaunched //解決bug：执行完onLaunch后再执行页面onLoad
    this.appType = option.type
    // await this.getAppId(option.type);
    this.lxUserInfo = this.cache.get('lxUserInfo')
  },

  data() {
    return {
      appType: 'oa',                 // 默认oa
      appId: '2621440-1638400',   // appId,默认oa
      systemKey:'',
      systemFlag:'yidong',
      lxUserInfo:{}
    }
  },
  created() {},
  async mounted() {
    // 正式部署删掉
    console.log("mounted===>: ")
//   await this.lx.getUserInfo(this.appId, this.appType)
//   this.lxUserInfo = this.cache.get('lxUserInfo')
  },
  computed: {},
  methods: {
    // 获取app信息
    getAppId(appType) {
      let appInfos = this.cache.get('appInfo');
      console.log('index-appInfos--->' + JSON.stringify(appInfos));
      if (appInfos == null || appInfos.length == 0) {
        this.$toast('获取app配置信息失败，请稍后再试！')
      }
      let appIds = appInfos.filter(item => item.type == appType);
      if (appIds != null && appIds.length > 0) {
        this.appId = appIds[0].appId;
      }
      this.cache.set("appId", this.appId);
      this.cache.set("appType", this.appType);
      console.log('index-appId--->' + this.cache.get("appId"));
      console.log('index-appType--->' + this.cache.get("appType"));
    },

    // 緩存全局用戶
    async onSubmit(){
      // 正式
      // await this.lx.getUserInfo()
      // let lxUserInfo = this.cache.get('lxUserInfo')
      // let userName = lxUserInfo.loginName
      // userName = userName.split('@')[1]
      // let userInfo = {
      //   systemKey: userName,
      //   systemFlag: 'yidong'
      // }
      // 测试
      let userInfo={
        systemKey: this.systemKey, // yinchengwei_hb lipan_hb xintianhe_hb jb_sun ls_liu zhangqi xm_ge jb_sun liuzhigang_hb
        systemFlag: 'yidong'
      }
      await this.getUserInfoByUserCode(userInfo)
      await this.redirectIndex()
    },

    /**
     * 通过用户userCode{lipan_hb}获取用户信息接口
     * @param {Object} userCode
     */
    async getUserInfoByUserCode(userInfo){
      await this.$http({
        url: this.$http.adornUrl(`/caac/oa/app/queryMainDept`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': userInfo.systemKey,
          'systemFlag': userInfo.systemFlag
        })
      }).then(({data}) => {
        if(data.code == 200){
          this.$set(data.data,'systemKey',userInfo.systemKey)
          this.$set(data.data,'systemFlag',userInfo.systemFlag)
          this.cache.set('userInfo',data.data)
        }
        console.log("用户信息-->data: " + JSON.stringify(data));
      })
    },

    // 根据不同蓝信用户跳转不同首页
    redirectIndex() {
      console.log("开始跳转--->")
      let deptName = this.cache.get('userInfo').deptName
      console.log("redirectIndex---" + JSON.stringify(this.cache.get('userInfo')))
      // 根据app类型跳转不同页
      this.common.toIndexByType(deptName, this.appType);
    },
  },
}
</script>

<style scoped>
  .content-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100vw;
    height: 100vh;
  }
  /deep/ .van-cell__title {
    min-width: 30%;
  }
</style>
